package com.yantong.service.impl;

import com.yantong.utils.PageUtils;
import com.yantong.pojo.Dispatch;
import com.yantong.pojo.DispatchExample;
import com.yantong.mapper.DispatchMapper;
import com.yantong.service.DispatchService;
import com.yantong.utils.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kanghaixin
 * @date 2018/9/2.10:12
 */
@Service
public class DispatchServiceImpl implements DispatchService{

    private static final Logger logger = LoggerFactory.getLogger(DispatchServiceImpl.class);
    @Autowired
    private DispatchMapper dispatchMapper;

    @Override
    public PageUtils<Dispatch> listDispatchs(String dispatchName, Integer status, int offset, int limit) {
        logger.info("listDispatchs, dispatchName:{}, status:{}", dispatchName, status);

        DispatchExample example = new DispatchExample();
        DispatchExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(dispatchName)) {
            criteria.andDispatchnameLike("%" + dispatchName + "%");
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        example.setOrderByClause("displayorder desc");
        PageUtils<Dispatch> pageUtils = new PageUtils<>();
        int total = dispatchMapper.countByExample(example);
        if (total >0) {
            pageUtils.setRows(dispatchMapper.selectByExample(example, new RowBounds(offset, limit)));
            pageUtils.setTotal(total);
        }
        return pageUtils;
    }

    @Override
    public List<Dispatch> getDispatchList(String dispatchName, Integer status, Long centreId, Long teamId) {
//        DispatchExample example = new DispatchExample();
//        DispatchExample.Criteria criteria = example.createCriteria();
//        if (StringUtils.isNotEmpty(dispatchName)) {
//            criteria.andDispatchnameLike(dispatchName);
//        }
//        if (status != null) {
//            criteria.andStatusEqualTo(status);
//        }
//        example.setOrderByClause("displayorder desc");
//        return dispatchMapper.selectByExample(example);
        Map<String, Object> params = new HashMap<>();
        if (ValidateUtil.isNotEmptyObjectOrString(dispatchName)){
            params.put("dispatchName",dispatchName);
        }
        if (ValidateUtil.isNotEmptyObjectOrString(status)){
            params.put("status",status);
        }
        if (ValidateUtil.isNotEmptyObjectOrString(centreId)){
            params.put("centreId",centreId);
        }
        if (ValidateUtil.isNotEmptyObjectOrString(teamId)){
            params.put("teamId",teamId);
        }
        return dispatchMapper.listPerms(params);
    }

    @Override
    public int updateDispatch(Dispatch dispatch) {
        if ( dispatch == null || dispatch.getId() == null) {
            logger.info("dispatch is null or dispatchId is null");
            return 0;
        }
        return dispatchMapper.updateByPrimaryKeySelective(dispatch);
    }

    @Override
    public int saveDispatch(Dispatch dispatch) {
        if (dispatch == null) {
            logger.info("dispatch is null");
            return 0;
        }
        return dispatchMapper.insertSelective(dispatch);
    }

    @Override
    public Dispatch getByKey(Long dispatchId) {
        if (dispatchId == null) {
            return null;
        }
        return dispatchMapper.selectByPrimaryKey(dispatchId);
    }

    @Override
    public int remove(Long dispatchId) {
        if (dispatchId == null) {
            return 0;
        }
        return dispatchMapper.deleteByPrimaryKey(dispatchId);
    }
}
